##############################################################
# Outcomes Mexico 2012 Panel Study traditional approach
##############################################################

rm (list = ls())

library(lmtest)
library(sandwich)
library(stargazer)

# load function that does clustered SEs
vcovCluster <- function(
  model,
  cluster
)
{
  require(sandwich)
  require(lmtest)
  if(nrow(model.matrix(model))!=length(cluster)){
    stop("check your data: cluster variable has different N than model")
  }
  M <- length(unique(cluster))
  N <- length(cluster)           
  K <- model$rank   
  if(M<40){
    warning("Fewer than 40 clusters, variances may be unreliable")
  }
  dfc <- (M/(M - 1)) * ((N - 1)/(N - K))
  uj  <- apply(estfun(model), 2, function(x) tapply(x, cluster, sum));
  rcse.cov <- dfc * sandwich(model, meat = crossprod(uj)/N)
  return(rcse.cov)
}

#########################
# Load and explore data
#########################

# Generate log  
#sink("009_outcomes_mexico_2012_traditional.txt")

# Read data
load("007_panel_mexico_2012_traditional_socio_2017jan30.RData")
d_traditional_socio =  d
d = NULL

# Read data 
load("008_panel_mexico_2012_traditional_ego_2017jan30.RData")
d_traditional_ego =  d
d = NULL

#################################
# Results traditional socio
#################################

mod_tra_socio = lm(d_traditional_socio$outcome  ~ d_traditional_socio$t_ind + 
                                                  d_traditional_socio$vote_incumbent + 
                                                  d_traditional_socio$ideology_imp + 
                                                  d_traditional_socio$vazquez_thermometer_imp + 
                                                  as.factor(d_traditional_socio$precint))
mod_tra_socio_c <- coeftest(mod_tra_socio, vcov = vcovCluster(mod_tra_socio, cluster = d_traditional_socio$precint))

mod_tra_ego = lm(d_traditional_ego$outcome  ~ d_traditional_ego$t_ind + 
                                              d_traditional_ego$vote_incumbent + 
                                              d_traditional_ego$ideology_imp + 
                                              d_traditional_ego$vazquez_thermometer_imp + 
                                              as.factor(d_traditional_ego$precint))
mod_tra_ego_c <- coeftest(mod_tra_ego, vcov = vcovCluster(mod_tra_ego, cluster = d_traditional_ego$precint))

##############
# Table LaTeX
##############

# TABLE 5 FIRST PART MANUSCRIPT 
dim(d_traditional_socio)
dim(d_traditional_ego)
stargazer(mod_tra_socio_c,mod_tra_ego_c,
          type = "text",
          title="Traditional approach",
          align=TRUE, 
          omit.stat=c("LL","ser","f","rsq","adj.rsq"), 
          no.space=TRUE,  
          multicolumn = TRUE,
          table.placement = "H",
          covariate.labels=c("Sociotropic perceptions (1-5)","Egotropic perceptions (1-5)"),
          dep.var.caption  = "Voting for the incumbent",
          dep.var.labels.include = FALSE,
          omit = c("vote_incumbent", 
                   "ideology_imp", 
                   "vazquez_thermometer_imp",
                   "precint",
                   "Constant"),
          add.lines = list(c("Controls","Yes","Yes"),
                           c("Neighborhood Fixed effects","Yes","Yes"),
                           c("Observations","916","918")))

#sink()
